package com.cqeec.waimaidemo.config;

import com.cqeec.waimaidemo.common.JacksonObjectMapper;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

@Configuration
public class MvcConfig extends WebMvcConfigurationSupport {
    @Override
//    http://localhost:8888/backend/index.html
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/backend/**")
                .addResourceLocations("classpath:/backend/");
        registry.addResourceHandler("/front/**")
                .addResourceLocations("classpath:/front/");
    }
//    扩展消息转换器

    @Override
    protected void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        MappingJackson2HttpMessageConverter  converter = new MappingJackson2HttpMessageConverter();
//        设置自定义objecMapper
        converter.setObjectMapper(new JacksonObjectMapper());
//        将新的消息转换器添加到原来到原来的消息转换器的开始
        converters.add(0,converter);

    }
}
